package com.breeze.toolkit.util;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

/**
 * Created by john on 2016/8/23.
 */
public class DialogUtil {
	private DialogUtil() {}
	
	public static Dialog showLoadingDialog(Context context) {
		return showLoadingDialog(context, context.getResources().getString(ResUtil.getStringId(context, ConstantsUtil.LOADING_STRING_NAME)));
	}

	public static Dialog showLoadingDialog(Context context, String message) {
		if (context != null) {
			View alertView = LayoutInflater.from(context).inflate(ResUtil.getLayoutId(context, ConstantsUtil.LOADING_DIALOG_LAYOUT_NAME), null);
			Dialog dialog = new Dialog(context, ResUtil.getStyleId(context, ConstantsUtil.LOADING_DIALOG_STYLE_NAME));
			dialog.setContentView(alertView);
			TextView msgText = (TextView) alertView.findViewById(ResUtil.getViewId(context, ConstantsUtil.LOADING_DIALOG_MSG_NAME));
			if (msgText != null) {
				msgText.setText(message);
			}
			dialog.setCanceledOnTouchOutside(false);
			dialog.setCancelable(true);
			try {
				dialog.show();
				return dialog;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	public static void dismissLoadingDialog(Dialog dialog) {
		if (dialog != null) {
			try {
				dialog.dismiss();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void cancelDialog(View v) {
		if (v.getTag() != null && (v.getTag() instanceof Dialog)) {
			try {
				((Dialog) v.getTag()).cancel();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
	}
}
